package oppty_status

import (
	"context"
	"git.mycaigou.com/gfyx/common/simplecopier"
	"git.mycaigou.com/gfyx/common/usermetadata"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	opptyStatusRepo "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/business_opportunity/oppty_status"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
	"strings"
)

type opptyStatusService struct {
	opptyStatusRepos        opptyStatusRepo.GfyxOpptyStatusReposIface
	opptyStatusHistoryRepos opptyStatusRepo.GfyxOpptyStatusHistoryReposIface
}

func NewOpptyStatusService(
	opptyStatusRepos opptyStatusRepo.GfyxOpptyStatusReposIface,
	opptyStatusHistoryRepos opptyStatusRepo.GfyxOpptyStatusHistoryReposIface,
) OpptyStatusServiceIface {
	return &opptyStatusService{
		opptyStatusRepos:        opptyStatusRepos,
		opptyStatusHistoryRepos: opptyStatusHistoryRepos,
	}
}

// GetOpptyStatus 获取联系人列表
func (f *opptyStatusService) GetOpptyStatus(ctx context.Context, request *pb.GetOpptyStatusRequest) (*pb.GetOpptyStatusResponse, error) {
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	// 特殊处理一下，以前的甲方和工程单位，都合并到了客户中
	if strings.EqualFold(request.DataSource, constants.OpptySourceDeveloper) || strings.EqualFold(request.DataSource, constants.OpptySourceSupplier) {
		request.DataSource = constants.OpptySourceCustomer
	}
	result, err := f.opptyStatusRepos.GetOpptyStatus(ctx, request.DataSource, request.DataSourceId, metaUserInfo.TenantUserUid)
	if err != nil {
		stark.Logger.Error(ctx, "GetOpptyStatus err", err)
		return nil, err
	}
	data := &pb.GetOpptyStatusResponse{}
	simplecopier.CopyToStruct(data, result)
	if data.Status == 0 && request.DetailPage { //如果是详情页，更新为已浏览
		param := &pb.UpdateOpptyStatusRequest{
			DataSourceId: request.DataSourceId,
			DataSource:   request.DataSource,
			Status:       1,
			FromDevice:   request.FromDevice,
		}
		f.UpdateOpptyStatus(ctx, param)
	}
	data.StatusName = constants.GetOpptyNameByStatus(data.Status) //f.getStatusName(data.Status)
	return data, nil
}

func (f *opptyStatusService) GetOpptyStatusBatch(ctx context.Context, request *pb.GetOpptyStatusBatchRequest) ([]*pb.GetOpptyStatusResponse, error) {
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	data := make([]*pb.GetOpptyStatusResponse, 0)
	// 特殊处理一下，以前的甲方和工程单位，都合并到了客户中
	if strings.EqualFold(request.DataSource, constants.OpptySourceDeveloper) || strings.EqualFold(request.DataSource, constants.OpptySourceSupplier) {
		request.DataSource = constants.OpptySourceCustomer
	}
	result, err := f.opptyStatusRepos.GetOpptyStatusBatch(ctx, request.DataSource, request.DataSourceId, metaUserInfo.TenantUserUid)
	if err != nil {
		stark.Logger.Error(ctx, "GetOpptyStatus err", err)
		return nil, err
	}
	for _, value := range result {
		item := &pb.GetOpptyStatusResponse{}
		simplecopier.CopyToStruct(item, value)
		item.StatusName = constants.GetOpptyNameByStatus(item.Status) // f.getStatusName(item.Status)
		data = append(data, item)
	}
	return data, nil
}

func (f *opptyStatusService) UpdateOpptyStatus(ctx context.Context, request *pb.UpdateOpptyStatusRequest) (bool, error) {
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	// 特殊处理一下，以前的甲方和工程单位，都合并到了客户中
	if strings.EqualFold(request.DataSource, constants.OpptySourceDeveloper) || strings.EqualFold(request.DataSource, constants.OpptySourceSupplier) {
		request.DataSource = constants.OpptySourceCustomer
	}

	bigDataOld, err := f.opptyStatusRepos.GetOpptyStatus(ctx, request.DataSource, request.DataSourceId, metaUserInfo.TenantUserUid)
	if err != nil {
		stark.Logger.Error(ctx, "GetOpptyStatus err", err)
		return false, err
	}
	if request.Status <= bigDataOld.Status { //如果更新状态，小于等于当前状态，则没必要更新，return
		return true, nil
	}
	if bigDataOld.Id == 0 {
		addData := &opptyStatusRepo.GfyxOpptyStatus{
			Status:       request.Status,
			DataSource:   request.DataSource,
			DataSourceId: request.DataSourceId,
			UserGuid:     metaUserInfo.TenantUserUid,
		}
		err := f.opptyStatusRepos.SaveOpptyStatus(ctx, addData)
		if err != nil {
			stark.Logger.Error(ctx, "SaveOpptyStatus err", err)
			return false, err
		}
	} else {
		_, err := f.opptyStatusRepos.UpdateStatus(ctx, request.DataSource, request.DataSourceId, metaUserInfo.TenantUserUid, request.Status)
		if err != nil {
			stark.Logger.Error(ctx, "UpdateOpptyStatus err", err)
			return false, err
		}
	}

	//状态变更历史表
	opptyStatus, err := f.opptyStatusRepos.GetOpptyStatus(ctx, request.DataSource, request.DataSourceId, metaUserInfo.TenantUserUid)
	if err != nil {
		stark.Logger.Error(ctx, "GetOpptyStatus err", err)
		return false, err
	}
	updateData := &opptyStatusRepo.GfyxOpptyStatusHistory{
		DataStatusId: opptyStatus.Id,
		BeforeStatus: bigDataOld.Status,
		AfterStatus:  request.Status,
		FromDevice:   request.FromDevice,
	}
	err = f.opptyStatusHistoryRepos.SaveOpptyStatusHistory(ctx, updateData)
	if err != nil {
		stark.Logger.Error(ctx, "SaveOpptyStatusHistory err", err)
		return false, err
	}
	return true, nil
}
